var content="";

			var ip="http://qure.zjgsu.edu.cn";
var idlist=new Array();
var url=decodeURI(window.location.search);
var C1=url.split("?")[1];//Id=5baef8585f44b96494ebf620&name=宣传部&group=中层领导
var C2=C1.split("&")[0];//Id=5baef8585f44b96494ebf620
var ID=C2.split("=")[1];
var T1=C1.split("&")[2];//group=中层领导
var ggroup=T1.split("=")[1];//中层领导
var N1=C1.split("&")[1];
var N2=N1.split("=")[1];//宣传部
var page=1; //设置首页页码
var size=25;
var searchKeyWords="";
var totalcount1=50;
var totalcount2=50;
var totalcount3=50;
//创建教师列表
function createTeacherList(){
	$("#peoplelist").append(
					'<div id=list1>' +

					'<table id="teacherInfo" class="display" cellspacing="0" width="100%" >\n' +
					'        <thead>\n' +
						'        <tr>\n' +
						'            <th width="10%"><input id="checkAll" class="checkAll1" type="checkbox" value=""></th>\n' +
						'            <th width="25%" id="th_worknumber" >工号</th>\n' +
						'            <th width="25%" id="th_tname" >姓名</th>\n' +
						'            <th width="25%" id="th_department">部门</th>\n' +
						'            <th width="25%" id="th_telenumber" >手机</th>\n' +
						
	
						'        </tr>\n' +
					'        </thead>\n' +
                    '        <tbody style="font-size: 14px">\n' +
					'        </tbody>\n' +
					'    </table>' +
					'</div>');

				
				
				 $('#teacherInfo').DataTable({
				 	responsive: true,
					searching:false,
			        "bLengthChange": false,
			        "bRetrieve": false,
			        'paging':false,
			        "bFilter": false, //过滤功能
			        info:false,
			        
					"columnDefs": [ 
						{ "orderable": false, "targets": 0 }
						
					]
				 });

}
//教师列表填充数据
function teacherTableDataAjax(pageconf1){
	if(!pageconf1){
		pageconf1={};
		pageconf1.pageSize=25;
		pageconf1.currentPage=1;
	}
	$.ajax({
		type:"get",
		url:ip+"/user?userTypeId=5b88e7e8dc8b5a1a84a96368&page="+pageconf1.currentPage+"&size="+pageconf1.pageSize+"&keyWord="+searchKeyWords,
		data:{},
		async:true,
		dataType:"json",
		contentType:" application/json",
		xhrFields:{withCredentials:true},
		crossDomain:true,
		success:function(res){
			console.log(res);
			$('#teacherInfo').dataTable().fnClearTable(); //清除表格内
			/*expressdata(obj.data);*/
			console.log(pageconf1);
			totalcount1=res.data.totalCount;
			console.log(totalcount1);
			getPage1(pageconf1,res.data.data);
		
	    },
		Error:function(){
			alert("服务器出错");
		}
		
	})
}




//教师列表填充数据的具体操作
function expressdata(data){
	var i=0;
	$.each(data, function(index, item) {
		            idlist[i]=item.id;
		            /*console.log(idlist[i]);*/
		            i++;
					$('#teacherInfo').dataTable().fnAddData([
						'<input class="addteacherbox" type="checkbox" name="addcheck" id="'+item.id+'">',
						item.username,
						item.name,
						item.collegeName,
						item.longPhone,
						
					]);
					
										
				});
				
}

//创建学生列表
function createStudentList(){
	$("#peoplelist").append(

					'<div id=list2>' +

					'<table id="studentInfo" class="display" cellspacing="0" width="100%" >\n' +
					'        <thead>\n' +
					'        <tr>\n' +
					'            <th width="2%"><input id="checkAll" class="checkAll2" type="checkbox" value=""></th>\n' +
					'            <th width="15%" id="th_stuid" >学号</th>\n' +
					'            <th width="15%" id="th_stuname" >姓名</th>\n' +
					'            <th width="10%" id="th_stusex">性别</th>\n' +
				
					'            <th width="25%" id="th_academy" >学院</th>\n' +
				
					'            <th width="10%" id="th_class" >班级</th>\n' +
					'            <th width="15%" id="th_stutele" >手机</th>\n' +
				

					'        </tr>\n' +
					'        </thead>\n' +
					
					'        <tbody style="font-size: 14px">\n' +
					'        </tbody>\n' +
					'    </table>' +
					'</div>');

	            // DataTable
				var table = $('#studentInfo').DataTable({
					ordering: false,
					'paging':false,
					searching:false,
					info:false
				});
}
//学生列表填充数据
function studentTableDataAjax(pageconf){
	if(!pageconf){
		pageconf={};
		pageconf.pageSize=25;
		pageconf.currentPage=1;
	}
	$.ajax({
		type:"get",
		url:ip+"/user?userTypeId=5b88e7a5dc8b5a1a84a96367&page="+pageconf.currentPage+"&size="+pageconf.pageSize+"&keyWord="+searchKeyWords,
		data:{
			
		},
		async:true,
		dataType:"json",
		contentType:" application/json",
		xhrFields:{withCredentials:true},
		crossDomain:true,
		success:function(obj){
			$('#studentInfo').dataTable().fnClearTable(); //清除表格内
			console.log(obj)
			totalcount2=obj.data.totalCount;
			getPage2(pageconf,obj.data.data);
		},
		Error:function(){
			alert("服务器出错");
		}
		
	})
	
	
	
	
}
//给学生列表
function expressstudata(data){
	
	$.each(data, function(index, item) {
					$('#studentInfo').dataTable().fnAddData([
						 '<input class="addstudentbox" type="checkbox" name="addcheck" id="'+item.id+'">',
						item.username,
						item.name,
						item.gender,
						
						item.collegeName,
						
						item.className,
						item.longPhone,
						
						
						
						
						
					]);
					
										
				});
				
	
	
}







//创建辅导员列表
function creatgraduatestudentList(){
	
	$("#peoplelist").append(
					'<div id=list3>' +

					'<table id="graduateInfo" class="display" cellspacing="0" width="100%">\n' +
					'        <thead>\n' +
					'        <tr>\n' +
					'            <th width="5%"><input id="checkAll" class="checkAll3" type="checkbox" value=""></th>\n' +
					'            <th width="10%" id="_class" >学号</th>\n' +
					'            <th width="10%" id="major" >姓名</th>\n' +
                    '            <th width="10%" id="level" >专业</th>\n' +
                    '            <th width="10%" id="level" >学院</th>\n' +
					'            <th width="20%" cotelenumber" >层次</th>\n' +
					'            <th width="25%" id="th_action" >手机号码</th>\n' +
                   
					'        </tr>\n' +
					'        </thead>\n' +
					'        <tbody style="font-size: 14px">\n' +
					'        </tbody>\n' +
					'    </table>' +
					'</div>');

	var table = $('#graduateInfo').DataTable({
					ordering: false,
					'paging':false,
					searching:false,
					info:false
				});			
	
	
	
}
//学生列表填充数据
function graduateTableDataAjax(pageconf){
	if(!pageconf){
		pageconf={};
		pageconf.pageSize=25;
		pageconf.currentPage=1;
	}
	$.ajax({
		type:"get",
		url:ip+"/user?userTypeId=5b88e7e8dc8b5a1a84a96370&page="+pageconf.currentPage+"&size="+pageconf.pageSize+"&keyWord="+searchKeyWords,
		data:{
			
		},
		async:true,
		dataType:"json",
		contentType:" application/json",
		xhrFields:{withCredentials:true},
		crossDomain:true,
		success:function(obj){
			$('#graduateInfo').dataTable().fnClearTable(); //清除表格内
			console.log(obj)
			totalcount3=obj.data.totalCount;
			getPage3(pageconf,obj.data.data);
		},
		Error:function(){
			alert("服务器出错");
		}
		
	})
	
	
	
	
}
function expressgradata(data){

	$.each(data, function(index, item) {
					$('#graduateInfo').dataTable().fnAddData([
						'<input class="addgraduatebox"type="checkbox" name="addcheck" id="'+item.id+'">',
						item.username,
						item.name,
						item.major,
						item.collegeName,
						item.level,
						item.longPhone,
						'<div><div class="layui-btn  layui-btn-small delete" style="float:left;" id="' + item.id+ '"  >删除</div><div class="layui-btn layui-btn-disabled layui-btn-small " style="float:right;"id="' + item.id+ '"  >禁用</div></div> '
					]);
				});
}


//批量添加人员
$(document).on('click', '.addall', function(){
	var peopleAddList=new Array();
	var i=0;
    $("input[name='addcheck']:checked").each(function(){
    	var number=this.id;
		peopleAddList[i]=number;
    	i++;
        $("input[type='checkbox']:checked").prop('checked', false)
    })
    $.ajax({
		type: "post",
		url: ip + "/surveyGroup/addUser",
		data: JSON.stringify({
			 _method:"post",
			"surveyGroupId":ID,
			"group":ggroup,
			"userIds":peopleAddList
		}),
		xhrFields: {
			withCredentials: true
		},
		crossDomain: true,
		async: true,
		dataType: "json",
		contentType: "application/json",
		success: function(res) {
			if(res.code==-1){
				layer.msg(res.msg);
			}
			if(res.code==0){
				layui.use('layer', function() {
					var layer = layui.layer;
					layer.confirm('添加成功！是否继续添加？', {
		 				btn: ['是！继续添加！', '否！前去查看人员组'],
		 				icon: 3,
		 				title: '提示',
		 				btn2:function(index,layero){
		 					window.location.href = "groupcheck.html?Id=" + ID+"&name="+N2+"&group="+ggroup;
		 				}
		 			})
				})
			}
			console.log(peopleAddList)
		},
		Error: function() {
			alert("服务器出错");
		}
	})
	
})
/
 //返回按钮
  			 $(document).on('click', '.back', function() {
  			 	window.location.href = "eachteam.html?Id=" +ID+"&name="+N2;
  			 })
//教师分页操作
function getPage1(pageconf1,data,type){
    layui.use(['laypage', 'layer'], function(){
  	var laypage = layui.laypage
  	,layer = layui.layer;
 		laypage.render({
           	elem: 'demo0',
           	count: totalcount1, //数据总数，从服务端得到
           	curr:pageconf1.currentPage,
			limit:pageconf1.pageSize,
            limits:[6,12,18,24],
			skip:true,
			first:"首页",
			last:"尾页",
			layout:['count','skip', 'prev', 'next'],  
            jump: function(obj, first){
                   console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
                    console.log(obj.limit); //得到每页显示的条数
                    page=obj.curr;  //改变当前页码
                    limit=obj.limit;
                    //首次不执行
                    if(!first){
                    	pageconf1.currentPage=obj.curr;
					    pageconf1.pageSize=obj.limit;
		    			teacherTableDataAjax(pageconf1);
		    			
		    		}
                }
            
            });
            console.log(data);
           
   				expressdata(data);
   
            
        });
    }
//学生分页操作
function getPage2(pageconf,data,type){
    layui.use(['laypage', 'layer'], function(){
  	var laypage = layui.laypage
  	,layer = layui.layer;
 		laypage.render({
           	elem: 'demo1',
           	count: totalcount2, //数据总数，从服务端得到
           	curr:pageconf.currentPage,
			limit:pageconf.pageSize,
            limits:[6,12,18,24],
			skip:true,
			first:"首页",
			last:"尾页",
			layout:['count','skip', 'prev', 'next'],  
            jump: function(obj, first){
                   console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
                    console.log(obj.limit); //得到每页显示的条数
                    page=obj.curr;  //改变当前页码
                    limit=obj.limit;
                    //首次不执行
                    if(!first){
                    	pageconf.currentPage=obj.curr;
					    pageconf.pageSize=obj.limit;
		    			
		    			studentTableDataAjax(pageconf);
		    		}
                }
            
            });
            console.log(data);
            expressstudata(data);
   
            
        });
    }
//研究生分页操作
function getPage3(pageconf,data,type){
    layui.use(['laypage', 'layer'], function(){
  	var laypage = layui.laypage
  	,layer = layui.layer;
 		laypage.render({
           	elem: 'demo2',
           	count: totalcount3, //数据总数，从服务端得到
           	curr:pageconf.currentPage,
			limit:pageconf.pageSize,
            limits:[6,12,18,24],
			skip:true,
			first:"首页",
			last:"尾页",
			layout:['count','skip', 'prev', 'next'],  
            jump: function(obj, first){
                   console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
                    console.log(obj.limit); //得到每页显示的条数
                    page=obj.curr;  //改变当前页码
                    limit=obj.limit;
                    //首次不执行
                    if(!first){
                    	pageconf.currentPage=obj.curr;
					    pageconf.pageSize=obj.limit;
		    			graduateTableDataAjax(pageconf);
		    		}
                }
            
            });
            console.log(data);
            expressgradata(data);
   
            
        });
    }


//获取搜索关键字
function getKeyword(){
	$('#search-form').autocomplete({
		width: 200,
		height: 25,
		onSubmit: function(text){
			searchKeyWords=text;
		    console.log(searchKeyWords);
		  	teacherTableDataAjax();
		    studentTableDataAjax();
		    graduateTableDataAjax();
		    
		}
	});
}

function isLogin(){
				$.ajax({
					 				type: "get",
					 				url:ip+"/user/type ",
					 				data: {},
					 				async: true,
					 				dataType: "",
					 				contentType: "application/json",
					 				xhrFields: {
									withCredentials: true
									},
									crossDomain: true,
					 				success: function(res) {
					 					if(res.code==-1){
					 						layui.use('layer', function(){
			  									var layer = layui.layer;
			  									layer.msg(res.msg);
											});     
					 						
					 						window.location.href="login.html";
					 						console.log(res.msg);
					 					}
									
					 				},
					 				Error: function() {
					 					alert("服务器出错");
					 				}
					 			})
}
$(document).ready(function(){
	isLogin();
	$("#groupnames").html(ggroup);
	$("#groups").html(N2+">>");
	$('#shaixuantitle').html("添加人员——"+N2+"."+ggroup);
	createTeacherList()
	teacherTableDataAjax()
	getKeyword()
	createStudentList()
	studentTableDataAjax()
	creatgraduatestudentList()
    graduateTableDataAjax()
	$("#list1").hide();
	$("#list2").hide();
	$("#list3").hide();
	$("#demo0").hide();
	$("#demo1").hide();
	$("#demo2").hide();
	$("#addall").hide();
	$("#demo").hide();
	$("#peoplegroup").change(function() {
					if($(this).val() == "老师") {
						$("#list2").hide();
					    $("#list3").hide();
						$("#list1").show();
						$("#demo0").show();
						$("#demo1").hide();
						$("#demo2").hide();
						$("#addall").show();
						$("#demo").show();
					}
					if($(this).val() == "本科生") {
						$("#list1").hide();
						$("#list3").hide();
						$("#list2").show();
						$("#demo1").show();
						$("#demo0").hide();
						$("#demo2").hide();
						$("#addall").show();
						$("#demo").show();
					}
					if($(this).val() == "研究生") {
						$("#list1").hide();
						$("#list2").hide();
						$("#list3").show();
						$("#demo1").hide();
						$("#demo0").hide();
						$("#demo2").show();
						$("#addall").show();
						$("#demo").show();
					}
					

				});
					$('.checkAll1').on('click', function () {
            if (this.checked) {
                $(this).attr('checked','checked')
                $(".addteacherbox").each(function () {
                    this.checked = true;
                });
            } else {
                $(this).removeAttr('checked')
                $(".addteacherbox").each(function () {
                    this.checked = false;
                });
            }
        });
        $('.checkAll2').on('click', function () {
            if (this.checked) {
                $(this).attr('checked','checked')
                $(".addstudentbox").each(function () {
                    this.checked = true;
                });
            } else {
                $(this).removeAttr('checked')
                $(".addstudentbox").each(function () {
                    this.checked = false;
                });
            }
        });
        $('.checkAll3').on('click', function () {
            if (this.checked) {
                $(this).attr('checked','checked')
                $(".addgraduatebox").each(function () {
                    this.checked = true;
                });
            } else {
                $(this).removeAttr('checked')
                $(".addgraduatebox").each(function () {
                    this.checked = false;
                });
            }
        });

    

	
		
	
				
})
	
